<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/12/24
 * Time: 15:09
 */

namespace app\crm\controller;

//发圈
use app\crm\model\CircleModel;
use app\crm\traits\Upload;
use think\Request;
use think\Validate;

class Circle extends BaseController
{

    use Upload;
    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function lists(Request $request)
    {
        $pageSize = $request->post('pageSize', 10);
        $list = CircleModel::pagination($pageSize);
        $page = $list->render();
        $list = $list->toArray();
        foreach ($list['data'] as $k => $v) {
            if ($v['imgs']) {
                $img = \json_decode($v['imgs'], true);
                $list['data'][$k]['imgs'] = $img;
            }
        }
        $this->assign('list', $list['data']);
        $this->assign('page', $page);
        return $this->fetch('list');
    }

    /**
     * 添加数据界面
     * @return mixed
     */
    public function addRecord()
    {
        return $this->fetch('circle-add');
    }

    /**
     * 添加数据
     * @param Request $request
     * @return \think\response\Json
     */
    public function insert(Request $request)
    {
        $data = [];
        $data['title'] = $request->post('title', '');
        $data['detail'] = $request->post('detail', '');
        $data['begin_time'] = $request->post('begin_time');
        $data['end_time'] = $request->post('end_time');
        $data['order_id'] = $request->post('order_id', 0);
        $data['gold'] = $request->post('gold', 0);
        $data['is_active'] = $request->post('is_active', 1);
        $data['imgs'] = isset($_POST['img']) ? $_POST['img'] : '';
        $validate = new Validate([
            'title' => 'require',
            'detail' => 'require',
            'begin_time' => 'require',
            'end_time' => 'require',
            'order_id' => 'require'
        ]);
        if (!$validate->check($data)) {
            return json(['code' => 0, 'msg' => $validate->getError()]);
        }
        $data['add_time'] = time();
        $data['begin_time'] = strtotime($data['begin_time']);
        $data['end_time'] = strtotime($data['end_time']);
        $data['imgs'] = json_encode($data['imgs']);
        $res = CircleModel::insert($data);
        $output = $res ? ['code' => 1, 'msg' => '添加成功'] : ['code' => 1, 'msg' => '添加失败,请重试'];
        return json($output);
    }

    /**
     * 编辑界面
     * @param Request $request
     * @return mixed|\think\response\Json
     */
    public function edit(Request $request)
    {
        $id = $request->get('id', '');
        if (!is_numeric($id)) {
            return json(['code' => 0, 'msg' => 'id格式不正确']);
        }
        $info = CircleModel::one(['id' => $id]);
        $img = json_decode($info['imgs'], true);
        $imgs = [];
        if ($img) {
            foreach ($img as $v) {
                $tmp = [];
                $tmp['name'] = $v;
                $tmp['url'] = $v;
                $imgs[] = $tmp;
            }
        };
        $this->assign('imgs', \json_encode($imgs));
        $this->assign('info', $info);
        return $this->fetch('circle-edit');
    }

    /**
     * 编辑操作
     * @param Request $request
     * @return \think\response\Json
     */
    public function editAct(Request $request)
    {
        $id = $request->get('id', '');
        if ($id <= 0) {
            return json(['code' => 0, 'msg' => 'id格式不正确']);
        }
        $data = [];
        $data['title'] = $request->post('title', '');
        $data['detail'] = $request->post('detail', '');
        $data['begin_time'] = $request->post('begin_time');
        $data['end_time'] = $request->post('end_time');
        $data['order_id'] = $request->post('order_id', 0);
        $data['gold'] = $request->post('gold', 0);
        $data['is_active'] = $request->post('is_active', 1);
        $data['imgs'] = isset($_POST['img']) ? $_POST['img'] : '';
        $validate = new Validate([
            'title' => 'require',
            'detail' => 'require',
            'begin_time' => 'require',
            'end_time' => 'require',
            'order_id' => 'require'
        ]);
        if (!$validate->check($data)) {
            return json(['code' => 0, 'msg' => $validate->getError()]);
        }
        $data['add_time'] = time();
        $data['begin_time'] = strtotime($data['begin_time']);
        $data['end_time'] = strtotime($data['end_time']);
        $data['imgs'] = json_encode($data['imgs']);
        $res = CircleModel::update(['id' => $id], $data);
        $output = $res ? ['code' => 1, 'msg' => '更新成功'] : ['code' => 1, 'msg' => '更新失败,请重试'];
        return json($output);
    }

    public function updateParam(Request $request)
    {
        $id = $request->post('id', '');
        $param = $request->post('param', '');
        $value = $request->post('value', '');
        $info = CircleModel::one(['id' => $id]);
        if (!$info) {
            return json(['code' => 0, 'msg' => '记录不存在']);
        }
        $res = CircleModel::update(['id' => $id], [$param => $value]);
        $output = $res ? ['code' => 1, 'msg' => '更新成功'] : ['code' => 0, 'msg' => '更新失败,请重试'];
        return json($output);
    }

    public function del(Request $request)
    {
        $id = $request->post('id', '');
        if ($id <= 0) {
            return json(['code' => 0, 'msg' => 'id格式不正确']);
        }
        $info = CircleModel::one(['id' => $id]);
        if (!$info) {
            return json(['code' => 0, 'msg' => '数据不存在']);
        }
        $res = CircleModel::del(['id' => $id]);
        $output = $res ? ['code' => 1, 'msg' => '删除成功'] : ['code' => 0, 'msg' => '删除失败,请重试'];
        return json($output);
    }

}